using System;
using System.Collections;
using UnityEngine;
using UnityEngine.UI;

namespace PKGames.AR
{
	public class FlashScreen : MonoBehaviour
	{
		[SerializeField]
		private Image _flashTexture;

		[SerializeField]
		private CanvasGroup _canvas;

		[SerializeField]
		private float _fadeInTime;

		[SerializeField]
		private AnimationCurve _fadeIn;

		[SerializeField]
		private float _fadeOutTime;

		[SerializeField]
		private AnimationCurve _fadeOut;

		[SerializeField]
		private Color _color;

		private void Start()
		{
			_flashTexture.gameObject.SetActive(value: false);
			_flashTexture.color = _color;
		}

		public void Flash(Action onFinishCallback = null)
		{
			FadeIn(delegate
			{
				FadeOut(onFinishCallback);
			});
		}

		public void FadeIn(Action onFinishCallback = null)
		{
			StartCoroutine(FadeInAsync(onFinishCallback));
		}

		private IEnumerator FadeInAsync(Action onFinishCallback)
		{
			float currentTime = 0f;
			_flashTexture.gameObject.SetActive(value: true);
			while (currentTime <= _fadeInTime)
			{
				_canvas.alpha = _fadeIn.Evaluate(currentTime / _fadeInTime);
				currentTime += Time.deltaTime;
				yield return null;
			}
			_canvas.alpha = 1f;
			onFinishCallback.Fire();
			yield return null;
		}

		public void FadeOut(Action onFinishCallback = null)
		{
			StartCoroutine(FadeOutAsync(onFinishCallback));
		}

		private IEnumerator FadeOutAsync(Action onFinishCallback)
		{
			float currentTime = 0f;
			_flashTexture.gameObject.SetActive(value: true);
			while (currentTime <= _fadeOutTime)
			{
				_canvas.alpha = 1f - _fadeOut.Evaluate(currentTime / _fadeOutTime);
				currentTime += Time.deltaTime;
				yield return null;
			}
			_canvas.alpha = 0f;
			_flashTexture.gameObject.SetActive(value: false);
			onFinishCallback.Fire();
			yield return null;
		}
	}
}
